package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dto.company.CompanyInfoAddDto;
import com.ruoyi.system.dto.company.CompanyInfoSearchDto;
import com.ruoyi.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CompanyInfo;
import com.ruoyi.system.service.ICompanyInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单位基本信息Controller
 *
 *
 * @date 2024-03-09
 */
@Api(tags = "单位基本信息API")
@RestController
@RequestMapping("/system/companyInfo")
public class CompanyInfoController extends BaseController
{
    @Autowired
    private ICompanyInfoService companyInfoService;

    /**
     * 查询单位基本信息列表
     */
    @ApiOperation("查询单位基本信息列表")
//    @PreAuthorize("@ss.hasPermi('system:companyInfo:list')")
    @PostMapping("/list")
    public AjaxResult<PageResult<CompanyInfo>> list(@RequestBody CompanyInfoSearchDto companyInfoSearchDto) {
        CompanyInfo companyInfo = new CompanyInfo();
        BeanUtils.copyProperties(companyInfoSearchDto, companyInfo);
        List<CompanyInfo> allItems = companyInfoService.selectCompanyInfoList(companyInfo);
        PageRequest pageRequest = new PageRequest(companyInfoSearchDto.getPageNum(), companyInfoSearchDto.getPageSize());
        PageResult<CompanyInfo> companyInfoPageResult = PaginationHelper.paginateList(allItems, pageRequest);
        return AjaxResult.success(companyInfoPageResult);
    }

    /**
     * 导出单位基本信息列表
     */
    @ApiOperation("导出单位基本信息列表")
//    @PreAuthorize("@ss.hasPermi('system:companyInfo:export')")
    @Log(title = "单位基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody CompanyInfoSearchDto companyInfoSearchDto)
    {
        CompanyInfo companyInfo = new CompanyInfo();
        BeanUtils.copyProperties(companyInfoSearchDto,companyInfo);
        List<CompanyInfo> list = companyInfoService.selectCompanyInfoList(companyInfo);
        ExcelUtil<CompanyInfo> util = new ExcelUtil<CompanyInfo>(CompanyInfo.class);
        util.exportExcel(response, list, "单位基本信息数据");
    }

    /**
     * 根据单位ID获取单位基本信息详细信息
     */
    @ApiOperation("根据ID获取单位基本信息详细信息")
//    @PreAuthorize("@ss.hasPermi('system:companyInfo:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult<CompanyInfo> getInfo(@ApiParam(value = "单位ID") @PathVariable("companyId") Long companyId)
    {
        return AjaxResult.success(companyInfoService.selectCompanyInfoById(companyId));
    }

    /**
     * 修改单位基本信息
     */
    @ApiOperation("修改单位基本信息")
//    @PreAuthorize("@ss.hasPermi('system:companyInfo:edit')")
    @Log(title = "单位基本信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult<String> edit(@RequestBody CompanyInfo companyInfo)
    {
        int result = companyInfoService.updateCompanyInfo(companyInfo);
        if(!(result >0)){
            return AjaxResult.success("不存在该单位，请先增加");
        }
        return AjaxResult.success("修改单位信息成功");
    }

    /**
     * 删除单位基本信息
     */
    @ApiOperation("删除单位基本信息")
//    @PreAuthorize("@ss.hasPermi('system:companyInfo:remove')")
    @Log(title = "单位基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult<String> remove(@ApiParam(value = "单位ID列表") @PathVariable Long[] companyIds)
    {
        companyInfoService.deleteCompanyInfoByIds(companyIds);
        return  AjaxResult.success("删除单位信息成功");
    }
}
